/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
package com.liferay.analystest.window;

import com.liferay.analystest.util.Const;

import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;

/**
 * Main Window of Analystest
 * 
 * @author Dante Wang
 */
public class MainWindow extends JFrame {

	/** Creates new form mainWindow */
	public MainWindow() {
		initComponents();
		init();
	}

	public void addChartTab(String title, Component c) {
		_tabPaneCharts.add(title, c);
		_tabPaneCharts.validate();
	}

	public int getSelectedChartTab() {
		return _tabPaneCharts.getSelectedIndex();
	}

	public void setToolBarActionListener(ActionListener al) {
		for (JButton button : _toolBarChartControlButtons) {
			button.addActionListener(al);
		}
	}

	private JButton createToolBarButton(
			String command, String title, String toolTip,
			String imageFileName) {

		JButton button = null;

		if (command != null) {
			button = new JButton();

			button.setActionCommand(command);
			button.setBorderPainted(false);

			if (toolTip != null) {
				button.setToolTipText(toolTip);
			}

			if (imageFileName != null) {
				URL imageFileUrl = getClass().getResource(imageFileName);

				if (imageFileUrl != null) {
					ImageIcon icon = new ImageIcon(imageFileUrl);
					button.setIcon(icon);
				}
			}
			else if (title != null) {
				button.setText(title);
			}
			else {
				button.setText("Button");
			}
		}

		return button;
	}

	private void init() {
		/* Set properties for the main window */
		this.setPreferredSize(new Dimension(1280, 800));
		this.setMinimumSize(new Dimension(1024, 720));
		this.setSize(new Dimension(1280, 800));
		this.setBackground(Color.WHITE);
		this.setTitle("Analystest!");
		this.setLayout(new BorderLayout());

		/* Init the ui controls */
		initChartTabPanel();
		initChartToolBar();

		this.pack();
		this.validate();
	}

	private void initChartTabPanel() {
		/* Init chart tab panel */
		_tabPaneCharts = new JTabbedPane();
		_tabPaneCharts.setTabPlacement(JTabbedPane.TOP);
		_tabPaneCharts.setBackground(Color.white);
		_tabPaneCharts.validate();

		this.add(_tabPaneCharts, BorderLayout.CENTER);
	}

	private void initChartToolBar() {
		_toolBarChartControlButtons = new ArrayList<>();
		_toolBarChartControl = new JToolBar();

		_toolBarChartControl.setOrientation(JToolBar.VERTICAL);
		_toolBarChartControl.setFloatable(false);

		JButton buttonRefresh = createToolBarButton(
			Const.COMMAND_CHART_TOOLBAR_BTN + "Refresh", "Refresh",
			"Refresh the current chart.", "../resources/refresh_32.png");
		
		JButton buttonSave = createToolBarButton(
			Const.COMMAND_CHART_TOOLBAR_BTN + "SaveChart", "Save...",
			"Save current chart to PNG file.", "../resources/download_32.png");

		_toolBarChartControlButtons.add(buttonRefresh);
		_toolBarChartControlButtons.add(buttonSave);

		for (JButton button : _toolBarChartControlButtons) {
			_toolBarChartControl.add(button);
		}

		this.add(_toolBarChartControl, BorderLayout.EAST);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

	// UI controls
	private JTabbedPane _tabPaneCharts;
	private JToolBar _toolBarChartControl;
	private List<JButton> _toolBarChartControlButtons;
	
}